package com.miwzn.base;

import com.miwzn.comm.ResponseEntity;
import com.miwzn.comm.constant.MyConstant;
import com.miwzn.comm.utils.Page;
import com.miwzn.comm.utils.validate.AddValidGroup;
import com.miwzn.comm.utils.validate.UpdateValidGroup;
import com.miwzn.comm.utils.validate.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Auther: wzn
 * @Date: 2018/6/27 10:37
 * @Description:
 */
public abstract class AbstractController<T, D extends BaseEntity, Q extends QueryEntity> extends AbstractBase<T, D, Q> {

    @Autowired
    private BaseService<T, D, Q> baseService;

    public AbstractController() {
        super();
    }

    /**
     * 根据ID获取
     *
     * @param id
     *
     * @return
     */
    public ResponseEntity getById(String id) {
        T entity = baseService.selectByPrimaryKey(id);
        return new ResponseEntity("查询成功").put("data", wrapD(entity));
    }

    /**
     * 实体查询页面
     *
     * @param entity
     *
     * @return
     */
    public ResponseEntity list(T entity) {
        //
        super.setFieldByT("delFlag", MyConstant.FLAG_N, entity);

        Page page = baseService.selectTPage(entity);
        //
        return new ResponseEntity("查询成功").put("page", page);
    }

    /**
     * 实体查询页面
     *
     * @param entity
     *
     * @return
     */
    public ResponseEntity list(Q entity) {
        //
        super.setFieldByQ("delFlag", MyConstant.FLAG_N, entity);

        Page page = baseService.selectTPage(entity);
        //
        return new ResponseEntity("查询成功").put("page", page);
    }


    /**
     * 实体查询页面
     *
     * @param entity
     *
     * @return
     */
    public ResponseEntity listD(Q entity) {
        //
        super.setFieldByQ("delFlag", MyConstant.FLAG_N, entity);

        Page page = baseService.selectDPage(entity);
        //
        return new ResponseEntity("查询成功").put("page", page);
    }

    /**
     * 添加
     *
     * @param t
     *
     * @return
     */
    public ResponseEntity add(T t) {
        //
        ValidatorUtils.validateEntity(t, AddValidGroup.class);
        baseService.insert(t);

        return ResponseEntity.success("添加成功");
    }

    /**
     * 添加
     *
     * @param d
     *
     * @return
     */
    public ResponseEntity add(D d) {
        //
        ValidatorUtils.validateEntity(d, AddValidGroup.class);

        baseService.insert(wrapT(d));
        return ResponseEntity.success("添加成功");
    }

    /**
     * 添加
     *
     * @param ds
     *
     * @return
     */
    public ResponseEntity adds(List<D> ds) {
        for (D d : ds) {
            add(d);
        }
        return ResponseEntity.success("添加成功");
    }

    /**
     * 更新
     *
     * @param t
     * @param id
     *
     * @return
     */
    public ResponseEntity update(T t, String id) {
        return this.update(t);
    }

    /**
     * 更新
     *
     * @param t
     *
     * @return
     */
    public ResponseEntity update(T t) {
        ValidatorUtils.validateEntity(t, UpdateValidGroup.class);

        baseService.updateByPrimaryKey(t);
        return ResponseEntity.success("更新成功");
    }

    /**
     * 更新
     *
     * @param d
     *
     * @return
     */
    public ResponseEntity update(D d) {
        return this.update(super.wrapT(d));
    }

    /**
     * 软删除，
     *
     * @param id
     *
     * @return
     */
    public ResponseEntity deleteFlag(String id) {
        try {
            //
            T entity = (T) super.tClass.newInstance();
            super.setFieldByT("id", id, entity);
            super.setFieldByT("delFlag", MyConstant.FLAG_Y, entity);

            baseService.updateByPrimaryKeySelective(entity);
        } catch (InstantiationException e) {
            logger.error(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            logger.error(e.getMessage(), e);
        }
        return ResponseEntity.success("删除成功");
    }


    /**
     * 软删除，
     *
     * @param ids
     *
     * @return
     */
    public ResponseEntity deleteFlag(String[] ids) {
        for (String id : ids) {
            this.deleteFlag(id);
        }
        return ResponseEntity.success("删除成功");
    }

    /**
     * 物理删除，
     *
     * @param id
     *
     * @return
     */
    public ResponseEntity delete(String id) {
        try {
            //
            T entity = (T) super.tClass.newInstance();
            super.setFieldByT("id", id, entity);
            baseService.deleteByPrimaryKey(entity);
        } catch (InstantiationException e) {
            logger.error(e.getMessage(), e);
        } catch (IllegalAccessException e) {
            logger.error(e.getMessage(), e);
        }
        return ResponseEntity.success("删除成功");
    }

    /**
     * 软删除，
     *
     * @param ids
     *
     * @return
     */
    public ResponseEntity delete(String[] ids) {
        for (String id : ids) {
            if (StringUtils.isNotBlank(id)) {
                this.delete(id);
            }
        }
        return ResponseEntity.success("删除成功");
    }

}
